/*
 *  Copyright 2019-2023 oyun
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.oyun.modules.medicine.rest;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.oyun.annotation.Log;
import com.oyun.modules.medicine.domain.AdministrativeOffice;
import com.oyun.modules.medicine.domain.HospitalizedCase;
import com.oyun.modules.medicine.mapper.AdministrativeOfficeMapper;
import com.oyun.modules.medicine.service.HospitalizedCaseService;
import com.oyun.modules.medicine.domain.vo.HospitalizedCaseQueryCriteria;
import com.oyun.utils.PageResult;
import lombok.RequiredArgsConstructor;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * @author 呼和
 * @date 2024-07-02
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "病例管理管理")
@RequestMapping("/api/hospitalizedCase")
public class HospitalizedCaseController {

    private final HospitalizedCaseService hospitalizedCaseService;


    private final AdministrativeOfficeMapper administrativeOfficeMapper;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('hospitalizedCase:list')")
    public void exportHospitalizedCase(HttpServletResponse response, HospitalizedCaseQueryCriteria criteria) throws IOException {
        hospitalizedCaseService.download(hospitalizedCaseService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询病例管理")
    @ApiOperation("查询病例管理")
    @PreAuthorize("@el.check('hospitalizedCase:list')")
    public ResponseEntity<PageResult<HospitalizedCase>> queryHospitalizedCase(HospitalizedCaseQueryCriteria criteria, Page<Object> page) {
        return new ResponseEntity<>(hospitalizedCaseService.queryAll(criteria, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增病例管理")
    @ApiOperation("新增病例管理")
    @PreAuthorize("@el.check('hospitalizedCase:add')")
    public ResponseEntity<Object> createHospitalizedCase(@Validated @RequestBody HospitalizedCase resources) {
        hospitalizedCaseService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改病例管理")
    @ApiOperation("修改病例管理")
    @PreAuthorize("@el.check('hospitalizedCase:edit')")
    public ResponseEntity<Object> updateHospitalizedCase(@Validated @RequestBody HospitalizedCase resources) {
        hospitalizedCaseService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除病例管理")
    @ApiOperation("删除病例管理")
    @PreAuthorize("@el.check('hospitalizedCase:del')")
    public ResponseEntity<Object> deleteHospitalizedCase(@RequestBody List<Long> ids) {
        hospitalizedCaseService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("getOfficeByHpId/{hpId}")
    @ApiOperation("根据医院id获取科室")
    public List<AdministrativeOffice> getOfficeByHpId(@PathVariable("hpId") Long hpId) {
        LambdaQueryWrapper<AdministrativeOffice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdministrativeOffice::getHpId, hpId);
        return administrativeOfficeMapper.selectList(wrapper);
    }

}
